/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 ChatClientImpl.cpp. Created on: 21.10.2010 18:17:54 
 */

#include "ChatClientImpl.hpp"

ChatClientImpl::ChatClientImpl(IChatService* poService, const std::string& sLogin, const std::string& sPassword) :
		m_poService( NULL ),
		m_poSession ( NULL ),
		m_poSessionEventsSkeleton ( NULL ),
		m_sLogin(sLogin),
		m_sPassword(sPassword),
		m_bDisconnected( true ) {
	setNewRefObject ( m_poService, poService );
	m_poSessionEventsSkeleton = new ChatEventsSyncSkeleton(this);
}

ChatClientImpl::~ChatClientImpl() {
	if(m_poSession!=NULL && !m_bDisconnected) {
		try {
			m_poSession->leaveFromChatRequest(NULL);
		}
		catch(const std::exception& ex) {}
	}
	releaseAndClear(m_poSession);

	releaseAndClear(m_poSessionEventsSkeleton);

	if(m_poService!=NULL) {
		m_poService->delAvailabilityListener(this);
	}
	releaseAndClear(m_poService);
}

void ChatClientImpl::onAvailable(IRemoteSmartObject* poRemoteObject) throw() {

}

void ChatClientImpl::onUnavailable(IRemoteSmartObject* poRemoteObject) throw() {

}

void ChatClientImpl::start() {
	m_poSession = m_poService->enterToChat(m_sLogin, m_sPassword, m_poSessionEventsSkeleton);
	m_bDisconnected = false;
	m_poService->addAvailabilityListener(this);

	std::wstring sTextInput;

	do {
		sTextInput = readTextLine("> ");
		if(!sTextInput.empty() && sTextInput != L"exit") {
			sendUserInput(sTextInput);
		}
		else
			m_bDisconnected = true;
	}
	while(!m_bDisconnected );

}

std::wstring ChatClientImpl::readTextLine(const std::string& sPrompt) {
	std::wstring sResult;
	std::cout << sPrompt;
	std::wcin >> sResult;
	return sResult;
}

void ChatClientImpl::sendUserInput(const std::wstring& sUserInputText) {
	if(!m_bDisconnected) {
		boost::intrusive_ptr < ChatMessage > poMessage ( new ChatMessage(), false );
		poMessage->setValue(sUserInputText);
		m_poSession->sendMessage(poMessage.get());
	}
}

void  ChatClientImpl::onSystemMessage(ChatMessage* message) throw(IOException, SmartBusSystemException) {
	std::wcout << std::endl << L"[SM]" << message->getValue() << std::endl << L"<";
}

void  ChatClientImpl::onUserMessage(const std::string& login ,  ChatMessage* message) throw(IOException, SmartBusSystemException) {
	std::wcout << std::endl << cpputils::utils::toWideString(login) << L":" << message->getValue() << std::endl << L"<";
}

void  ChatClientImpl::onSessionWasClosed() throw(IOException, SmartBusSystemException) {
	std::wcout << std::endl << L"[SM] Session is closed by server" << std::endl << L"<";
}
